/**
 * Proprietary software owned by www.myatech.cn.All Rights reserved.
 * 软件版权归上海麦涯信息科技有限公司(www.myatech.cn)所有。
 */
package mya.mes.mdc.module;

import mya.common.config.ConfigBuilder;
import mya.common.config.ConfigUtil;
import mya.common.model.base.MyaModule;
import mya.mes.mdc.logic.MdcServiceFactoryImpl;
import mya.mes.mdc.logic.init.MdcInitServiceImpl;
import mya.mes.mdc.mongodb.Mongo;
import mya.mes.mdc.mqtt.MqttClientManager;
import mya.mes.mdc.share.api.MdcModule;
import mya.mes.mdc.share.vo.MdcOptions;
import mya.mes.mdc.weixin.WxUploadManager;
import org.apache.commons.configuration2.Configuration;

/**
 * @author leixu
 */
public class MesMdcModule implements MyaModule {

    public MesMdcModule() {
        MdcModule.getInstance().setMdcInitService(new MdcInitServiceImpl());
    }

    @Override
    public String module() {
        return "mdc";
    }

    @Override
    public String restPkg() {
        return "mya.mes.mdc.module.rest;mya.mes.mdc.internal.ui";
    }

    @Override
    public String pkgPrefix() {
        return "mya.mes.mdc";
    }

    @Override
    public Integer getInitIndex() {
        return 2;
    }

    @Override
    public void initModule() throws Exception {
        MdcModule.initServiceFactory(new MdcServiceFactoryImpl());
        init();
    }


    public void init() throws Exception {
        Configuration config = ConfigUtil.getInstance().loadModuleOption("mdc");
        if (config == null) {
            return;
        }

        ConfigBuilder<MdcOptions> builder = new ConfigBuilder<>(MdcOptions.class);

        builder.addClzProperty("MQTT_RECEIVE_PROCESSOR");

        MdcOptions mdcOptions = new MdcOptions();

        boolean buildOk = builder.build(config, mdcOptions);

        if (buildOk) {
            MdcModule.getInstance().init(mdcOptions);
        }

    }

    @Override
    public void shutdown() throws Exception {
        WxUploadManager.getInstance().shutdown();

        MqttClientManager.getInstance().shutdown();

        Mongo.getInstance().shutdown();
    }
}
